package com.example.administrator.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016/3/14 0014.
 */
public class MyAdapter extends BaseAdapter{
    private Context context;
    LayoutInflater inflater;
    private List<News> mNewsList;
    public MyAdapter(Context context,List<News> newsList){
        this.inflater = LayoutInflater.from(context);
        this.mNewsList = newsList;
    }
    @Override
    public int getCount() {
        return mNewsList.size();
    }

    @Override
    public Object getItem(int position) {
        return mNewsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /*
        listView 的优化：减少加载布局文件的次数
         */
        //将布局加载成View

        View view = inflater.inflate(R.layout.item_list2,null);

        //找到布局上的控件
        ImageView mImageView = (ImageView) view.findViewById(R.id.imageView1);
        TextView  mTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView mSummary = (TextView) view.findViewById(R.id.tv_content);
        //动态赋值
        News news = mNewsList.get(position);
        mImageView.setImageResource(news.getPicId());
        mTitle.setText(news.getTitle());
        mSummary.setText(news.getSummary());
        return view;
    }
}
